package com.hmdp.service.impl;/**
 * ClassName:UVServiceImpl
 * Package:com.hmdp.service.impl
 * Description:
 *
 * @Date:2022/4/26 13:15
 * @Author:lzp
 */

import com.hmdp.dto.Result;
import com.hmdp.service.IUVService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.hmdp.utils.RedisConstants.UV_KEY_PRE;

/**
 * ClassName:UVServiceImpl
 */
@Slf4j
@Service
public class UVServiceImpl implements IUVService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getCounts() {
        // 1.获取当前日期
        LocalDateTime now_date = LocalDateTime.now();
        // 2.将日期格式化，作为Redis中UV的后缀
        String now_suffix = now_date.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        String key_uv = UV_KEY_PRE + now_suffix;
        // 3.统计数量
        Long size = stringRedisTemplate.opsForHyperLogLog().size(key_uv);
        // 拆箱，防止null报错
        if(size != null){
            return Result.ok(size);
        }else{
            return Result.ok(0L);
        }

    }
}
